package com.iring.controller.admin;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.iring.controller.BaseController;
import com.iring.domain.common.Location;
import com.iring.domain.common.User;
import com.iring.vo.common.BooleanResultVO;
import com.iring.vo.common.LocationVO;
import com.iring.vo.common.ResultVO;
import com.iring.vo.user.ImgVO;
import com.iring.vo.user.PasswordVO;
import com.iring.vo.user.UserUpdateVO;

/**
 * 用户信息Controller
 *
 * @author wanggan
 */
@RestController
@RequestMapping(value = "/admin/user", produces = { "application/json;charset=UTF-8" })
public class UserController extends BaseController {

    /**
     * 获取用户账号信息
     *
     * @return 返回用户信息
     */
    @RequestMapping(value = "/get-user-info", method = RequestMethod.GET)
    public User getUserinfo() {
        User user = userService.getUserByUsername(getUserSession().getUsername());
        user.setPassword(null);
        user.setContactPhonenum(commonUtil.dealContactPhoneNumber(user.getContactPhonenum()));
        return user;
    }

    /**
     * 获取所有的地理位置信息
     *
     * @param locationVO
     *            {@link LocationVO}
     * @return 返回所有国家地理信息
     */
    @RequestMapping(value = "/list-all-location", method = RequestMethod.POST)
    public List<Location> listAllLocation(@RequestBody LocationVO locationVO) {
        return locationService.getByParentId(locationVO.getParentId());
    }

    /**
     * 查找省份或城市信息
     *
     * @param locationVO
     *            {@link LocationVO}
     * @return 返回省份或城市信息
     */
    @RequestMapping(value = "/list-all-location-province-or-city", method = RequestMethod.POST)
    public List<Location> listAllLocationProvinceOrCity(@RequestBody LocationVO locationVO) {
        return locationService.getByParentId(locationVO.getParentId());
    }

    /**
     * 用户修改个人信息
     *
     * @param userUpdateVO
     *            用户的信息 必须有联系人,地址,电话,用户名,组织字段
     *            {@link UserUpdateVO}
     * @return 修改结果
     */
    @RequestMapping(value = "/update-user-info", method = RequestMethod.POST)
    public BooleanResultVO updateUserinfo(@RequestBody UserUpdateVO userUpdateVO) {
        User user = userService.updateUser(getUserSession().getUsername(), userUpdateVO);
        if (user instanceof User) {
            // 重新设置session
            reSetUserSession(user);
            logService.saveLog(user.getUsername(),"update-user-info",getUserRealIp(),true);
            return new BooleanResultVO(Boolean.TRUE);
        } else {
            logService.saveLog(user.getUsername(),"update-user-info",getUserRealIp(),false);
            return new BooleanResultVO(Boolean.FALSE);
        }
    }

    /**
     * 修改密码
     *
     * @param passwordVO
     *            {@link PasswordVO}
     * @return 更新结果
     */
    @RequestMapping(value = "/update-password", method = RequestMethod.POST)
    public BooleanResultVO updatePassword(@RequestBody PasswordVO passwordVO) {
        String username = getUserSession().getUsername();
        Boolean result = userService.changePassword(passwordVO, username);
        logService.saveLog(username,"update-password",getUserRealIp(),true,"modify password after login");
        return new BooleanResultVO(result);
    }

    /**
     * 上传用户的头像进行修改
     *
     * @param imgVO
     *            {@link ImgVO}
     * @return 成功返回头像地址
     */
    @RequestMapping(value = "/update-head-img", method = RequestMethod.POST)
    public ResultVO updateHeadImg(@RequestBody ImgVO imgVO) {
        User user = userService.uploadHeadImg(imgVO, getUserSession().getUsername());
        // 设置session用户头像
        reSetUserSession(user);
        return new ResultVO(user.getHeadPicture());
    }

    /**
     * 修改注册使用的电话号码
     *
     * @return 返回用户的电话号码
     */
    @RequestMapping(value = "/send-valid-code", method = RequestMethod.GET)
    public ResultVO sendValidCode() {
        User user = userService.getUserByUsername(getUserSession().getUsername());
        return new ResultVO(commonUtil.dealContactPhoneNumber(user.getContactPhonenum()));
    }

}
